package com.jy.appmarket.cards;

import com.jy.appmarket.R;
import com.jy.appmarket.model.ApkFileInfo;

import android.content.Context;
import android.text.format.Formatter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import it.gmariotti.cardslib.library.internal.Card;

public class ApkFileUICard extends Card {
	protected TextView mApkName;
	protected TextView mApkSize;
	protected TextView mApkVersion;
	protected CheckBox mSelect;
	protected ImageView mApkIcon;
	protected TextView mDescription;
	protected ApkFileInfo mFileInfo;
	protected ApkFileUICheckBoxClickCallBack mListener;

	public interface ApkFileUICheckBoxClickCallBack {
		void onApkFileUICheckBoxClick(ApkFileInfo fileInfo);
		void onApkFileUICardClick(ApkFileInfo fileInfo);
	}

	
	public ApkFileUICard(Context context, ApkFileInfo fileInfo, ApkFileUICheckBoxClickCallBack callBack) {
		super(context, R.layout.apk_file_ui_card_layout);
		mFileInfo = fileInfo;
		mListener = callBack;
		setOnClickListener(new OnCardClickListener() {
			
			@Override
			public void onClick(Card card, View view) {
				mListener.onApkFileUICardClick(mFileInfo);
			}
		});
	}

	

	@Override
	public void setupInnerViewElements(ViewGroup parent, View view) {
		mApkName = parent.findViewById(R.id.apk_file_maneger_file_name);
		mApkSize = parent.findViewById(R.id.apk_file_maneger_file_size);
		mApkVersion = parent.findViewById(R.id.apk_file_maneger_file_versoin);
		mApkIcon = parent.findViewById(R.id.apk_file_maneger_file_icon);
		mSelect = parent.findViewById(R.id.apk_file_maneger_check_box);
		mDescription = parent.findViewById(R.id.apk_file_maneger_file_description);
		
		mApkName.setText(mFileInfo.getFileName());
		mApkSize.setText("大小："+Formatter.formatFileSize(getContext(), mFileInfo.getFileSize()));
		mApkVersion.setText("版本：V" + mFileInfo.getFileVersionName());
		mDescription.setText(mFileInfo.getDescription());
		//mApkIcon.setBackground(mFileInfo.getFileIcon());
		mApkIcon.setImageDrawable(mFileInfo.getFileIcon());
		mSelect.setFocusable(true);
		mSelect.setEnabled(true);
		mSelect.setChecked(mFileInfo.getSelect());
		
		mSelect.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mFileInfo.setSelect(mSelect.isChecked());
				if (mListener != null) {
					mListener.onApkFileUICheckBoxClick(mFileInfo);
				}
			}
		});
//		mSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//				mFileInfo.setSelect(isChecked);
//				if (mListener != null) {
//					mListener.onApkFileUICheckBoxClick(mFileInfo);
//				}
//			}
//		});
	}

}
